package com.housediyadmin.view.components
{   	
	import com.housediyadmin.model.BrandModel;
	
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	
	import org.robotlegs.mvcs.Mediator;
	
	
	/**
	 * 
	 * @author yanghanbing
	 * 2011/2/27
	 */ 
	public class FittingBrandCompMediator extends Mediator
	{
		[Inject]
		public var view:FittingBrandComp;
		
		[Inject]
		public var model:BrandModel;
		
		override public function onRegister():void
		{
			
			view.addBtn.addEventListener(MouseEvent.CLICK, changeToAddViewHandler);
			view.editOkBtn.addEventListener(MouseEvent.CLICK, changeToEditViewHandler);
			switch(view.id)
			{
				case "fittingList":
				    view.nameLab.text = "商品配套信息";
					view.brandList.visible = false;
					break;
				case "brandList":
					view.fittingList.visible = false;
					BindingUtils.bindProperty(view.brandList, "dataProvider", model, "brandArray");
					view.nameLab.text = "品牌信息";
					break;
			}
		}
		
       private function changeToAddViewHandler(event:MouseEvent):void
		{
		   view.currentState = "edit";
			switch(view.id)
			{
				case "fittingList":
					view.nameLab.text = "商品配套信息";
					view.editOkBtn.addEventListener(MouseEvent.CLICK, addFittingHandler);
					view.deleteCancleBtn.addEventListener(MouseEvent.CLICK, cancleHandler);
					break;
				case "brandList":
					view.nameLab.text = "品牌信息";
					view.editOkBtn.addEventListener(MouseEvent.CLICK, addBrandHandler);
					break;
			}
		}
	   
	   private function changeToEditViewHandler(event:MouseEvent):void
	   {
		   view.currentState = "edit";
		   switch(view.id)
		   {
			   case "fittingList":
				   view.editOkBtn.addEventListener(MouseEvent.CLICK, addFittingHandler);
				   view.deleteCancleBtn.addEventListener(MouseEvent.CLICK, cancleHandler);
				   break;
			   case "brandList":
				   BindingUtils.bindProperty(view.brandList, "dataProvider", model, "brandArray");
				   view.editOkBtn.addEventListener(MouseEvent.CLICK, addBrandHandler);
				   break;
		   }
	   }
	   
		private function addFittingHandler(event:MouseEvent):void
		{
			view.currentState = "normal";
			view.editOkBtn.removeEventListener(MouseEvent.CLICK, addFittingHandler);
			Alert.show("fitting");
		}
		
		private function cancleHandler(event:MouseEvent):void
		{
			view.currentState = "normal";
			view.editOkBtn.removeEventListener(MouseEvent.CLICK, addFittingHandler);
			view.deleteCancleBtn.removeEventListener(MouseEvent.CLICK, cancleHandler);
		}
		
		private function addBrandHandler(event:MouseEvent):void
		{
			view.currentState = "normal";
			view.editOkBtn.removeEventListener(MouseEvent.CLICK, addBrandHandler);
			Alert.show("brand");
		}
	}
}